package cn.aib.eduservice.service.impl;

import cn.aib.eduservice.entity.EduSubject;
import cn.aib.eduservice.entity.excel.SubjectData;
import cn.aib.eduservice.entity.subject.OneSubject;
import cn.aib.eduservice.entity.subject.TwoSubject;
import cn.aib.eduservice.listener.SubjectExcelListener;
import cn.aib.eduservice.mapper.EduSubjectMapper;
import cn.aib.eduservice.service.EduSubjectService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-12-20
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file,EduSubjectService service) {

        try {
            //文件的输入流
            InputStream in = file.getInputStream();
            //监听器
            SubjectExcelListener listener = new SubjectExcelListener(service);

            //读excel的数据到数据库
            EasyExcel.read(in, SubjectData.class,listener).sheet().doRead();

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询所有的一级节点
        //设置查询一级节点的条件,只要parent_id=0就是一级节点
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<EduSubject>();
        wrapper.eq("parent_id",0);

        //查到所有的一级节点课程分类，但是这并不是我们想返回的数据格式
        List<EduSubject> oneSubjects = baseMapper.selectList(wrapper);

        //查询所有的二级节点
        //设置查询二级节点的条件,只要parent_id!=0就是二级节点
        QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<EduSubject>();

        wrapper.ne("parent_id",0);

        //查到所有的二级节点课程分类，但是这并不是我们想返回的数据格式
        List<EduSubject> twoSubjects = baseMapper.selectList(wrapper2);

        //创建最终要返回的集合对象
        List<OneSubject> onelist = new ArrayList<>();
        //遍历一级节点进行数据的转换，并添加到List<OneSubject>
        for(int i=0;i<oneSubjects.size();i++){
            //数据格式的转换
            OneSubject oneSubject = new OneSubject();

            EduSubject eduSubject = oneSubjects.get(i);

//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());

            //将eduSubject的属性值复制到oneSubject中，只要属性名一致就可以
            BeanUtils.copyProperties(eduSubject, oneSubject);

            //创建List<TwoSubject>
            List<TwoSubject> twoList = new ArrayList<>();
            //遍历二级节点，找出匹配的子节点
            for(int j=0;j<twoSubjects.size();j++){

                EduSubject subject = twoSubjects.get(j);

                //当二级节点的父ID等于当前一级节点的ID时
                if( subject.getParentId().equals(oneSubject.getId()) ){
                    //数据的交换
                    TwoSubject twoSubject = new TwoSubject();

                    BeanUtils.copyProperties(subject, twoSubject);
                    //将二级节点添加到List<TwoSubject>
                    twoList.add(twoSubject);
                }
            }
            //twoList设置到oneSubject
            oneSubject.setChildren(twoList);

            //添加OneSubject到List<OneSubject>
            onelist.add(oneSubject);
        }

        return onelist;
    }

}
